package com.shitu.soulemo.main;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;
import com.shitu.base.BaseActivity;
import com.shitu.base.view.FixedViewPager;
import com.shitu.soulemo.R;
import com.shitu.soulemo.a.AFragment;
import com.shitu.soulemo.b.BFragment;
import com.shitu.soulemo.c.CFragment;
import com.shitu.soulemo.d.DFragment;
import com.shitu.soulemo.rn.RNApplyReferrer;
import com.shitu.soulemo.rn.RNReleaseOrderActivity;
import com.shitu.soulemo.utils.AccountHelper;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    private FixedViewPager pager;
    private BottomBar bar;

    private AFragment aFragment;
    private BFragment bFragment;
    private CFragment cFragment;
    private DFragment dFragment;

    private ImageView add;
    private Dialog dialog;
    private LinearLayout popup;
    private LinearLayout fabu;
    private LinearLayout apply;

    private String[] pages = new String[]{"发布需求单", "申请成为引荐人"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = findViewById(R.id.viewpager);
        bar = (BottomBar) findViewById(R.id.bottombar);
        add = findViewById(R.id.add);
        popup = findViewById(R.id.dialog);
        fabu = findViewById(R.id.fabu);
        apply = findViewById(R.id.apply);

        AccountHelper.getType();

        initViews();
    }

    private void initViews() {
        bFragment = new BFragment();
        cFragment = new CFragment();
        aFragment = new AFragment();
        dFragment = new DFragment();
        ArrayList<Fragment> list = new ArrayList<Fragment>();
        list.add(aFragment);
        list.add(bFragment);
        list.add(cFragment);
        list.add(dFragment);
        MainFragmentAdapter adapter = new MainFragmentAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);

        bar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                switch (tabId) {
                    case R.id.tab_a:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.tab_b:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.tab_c:
                        pager.setCurrentItem(2);
                        break;
                    case R.id.tab_d:
                        pager.setCurrentItem(3);
                        break;
                }
            }
        });

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                bar.selectTabAtPosition(i, true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RNReleaseOrderActivity.start(MainActivity.this);
            }
        });

        apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RNApplyReferrer.start(MainActivity.this);
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popup.getVisibility() == View.VISIBLE) {
                    popup.setVisibility(View.GONE);
                } else {
                    popup.setVisibility(View.VISIBLE);
                }
            }
        });

//        add.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                dialog = new AlertDialog.Builder(MainActivity.this).setTitle("选择跳转页面").setItems(pages, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        if (which == 0) {
//                            RNReleaseOrderActivity.start(MainActivity.this);
//                        } else {
//                            RNApplyReferrer.start(MainActivity.this);
//                        }
//                    }
//                }).create();
//                dialog.show();
//            }
//        });
    }

    @Override
    protected void onPause() {
        popup.setVisibility(View.GONE);
        super.onPause();
    }
}
